#ifndef __Relay_Control_C
#define __Relay_Control_C

#include "Relay_Control.h"
#include "SQDriConfig.h"

void Rly_init(void){	  
	  GPIO_Config_T  configStruct;
    RCM_EnableAPB2PeriphClock(Rly_RTC);	
	  #if RlyPin_Ctrl_PWM_EN == 0
			configStruct.pin = Rly1_PinId;
			configStruct.mode = GPIO_MODE_OUT_PP;
			configStruct.speed = GPIO_SPEED_50MHz;
			GPIO_Config(Rly_PinPort, &configStruct);
			// 初始化置位
			Rly_PinPort -> BC = Rly1_PinId;
		
			#if Has_RlyPin_Maintaining > 0
				RCM_EnableAPB2PeriphClock(Rly2_RTC);
				configStruct.pin = Rly2_PinId;
				GPIO_Config(Rly2_PinPort, &configStruct);	
				// 初始化置位
				Rly2_PinPort -> BC = Rly2_PinId;
			#endif
	  #else 
	    // 重定向引脚
	    #if defined SQDri_EVSEB_7KW || defined SQDri_EVSEB_11KW	    
      RCM_EnableMUXPeriphClock(Rly_IOMUX_RTC);	       			
	    GPIO_ConfigPinRemap(GPIO_REMAP_TMR4);
			#endif
	    // 初始化引脚			
		  configStruct.pin = Rly1_PinId;
			configStruct.mode = GPIO_MODE_AF_PP;
			configStruct.speed = GPIO_SPEED_50MHz;
			GPIO_Config(Rly_PinPort, &configStruct);		
		#endif
		
}

void Rly_on(){
	  #if RTC_ON > 0
		getTimestampRTC();
		#endif
	  if(chgForbid == 0 && system_time - Relay_Chg_tm >= relay_stable_time){
        #if Non_Plug_And_Charge > 0			  
			  if(startChgExternalReason.chargeControl == 0)  return;
				#elif PlugCharge_And_Resv
				if(resv_data.resv_time[0] > 1 && startChgExternalReason.chargeControl == 0) return;
        #endif
        #if RlyPin_Ctrl_PWM_EN == 0		  
					Rly_PinPort -> BSC = Rly1_PinId;
					#if Has_RlyPin_Maintaining > 0
						Rly2_PinPort -> BSC = Rly2_PinId;			  
					#endif
			  #else
			    Relay_Duty_Set(Relay_On_Duty);
			  #endif
        			
				// 记录继电器闭合时间;				
				if(Rly_status != 1) Relay_Chg_tm  = system_time;
				// 记录充电数据：
				if(chg_start_tm == 0){
						chg_start_tm  = system_time;
					  chg_stage = CHG_StartUP;
				}        				
				// 充电日志保存
				#if LOG_ON > 0
				if(Rly_status != 1){						
						log_temp.occTime  = system_time / 1000;
						log_temp.logGrade = Log_Info;                // 信息级别;
						log_temp.logEvent = logEvt_ChargeStart;      // 开启充电;
						log_temp.logEvtData = 0;                     // 充电数据;
						if(log_data_append(log_temp)){
							  writeData[FWD_LogsSize_idx].intData = (uint32_t)Log_Saved_Sum; // 更新Log总数
								// 数据保存
								flash_write();
								flash_log_write();
						}						
				}					
				#endif
				// 插座状态
				Rly_status = 1;
		}
}

void Rly_off(){
	  #if RTC_ON > 0
		getTimestampRTC();
		#endif
    if(system_time - Relay_Chg_tm >= relay_stable_time){
			  #if RlyPin_Ctrl_PWM_EN == 0
					Rly_PinPort -> BC = Rly1_PinId; //置0
					#if Has_RlyPin_Maintaining > 0
						Rly2_PinPort -> BC = Rly2_PinId;				
					#endif
			  #else
			    Relay_Duty_Set(Relay_Off_Duty);
			  #endif
        #if Has_RlyPin_Maintaining > 0			
					Rly_startup_status = 0;	
//					chg_stage = CHG_STOP;	
			  #endif
			
				// 继电器关闭：				
				if(Rly_status != 0) Relay_Chg_tm  = system_time;
				// 日志记录
				#if LOG_ON > 0
				if(Rly_status != 0){				
						log_temp.occTime  = system_time / 1000;
						log_temp.logGrade = Log_Info;               // 信息级别;
						log_temp.logEvent = logEvt_ChargeStop;      // 关闭充电;
						log_temp.logEvtData = 0;                    // 相关数据;
						if(log_data_append(log_temp)){
								writeData[FWD_LogsSize_idx].intData = (uint32_t)Log_Saved_Sum; // 更新Log总数
								// 数据保存
								flash_write(); 
								flash_log_write();	
						}							
				}
				#endif
				// 插座状态
				Rly_status = 0;
				// 关机后所有原因归0：
				// 自锁按键不可恢复0状态：
        #if Non_Plug_And_Charge > 0 || PlugCharge_And_Resv
					#if Key_SelfLock > 0 && key1_Ctrl_id == KeyCtrl_startChg
						startChgExternalReason.chargeControl = startChgExternalReason.chargeControl & 0x01;			
					#else
						startChgExternalReason.chargeControl = 0;
					#endif		
        #endif 						
		}		  
}

#if Has_RlyPin_Maintaining > 0
// 关闭启动引脚
void Rly_release_startup_Pin(){
	  #if RlyPin_Ctrl_PWM_EN == 0
	    Rly_PinPort -> BC = Rly1_PinId; //置0  
	  #else
	    Relay_Duty_Set(Relay_persist_Duty);
	  #endif
    // 启动引脚 Rly1_PinId 释放标志;
		Rly_startup_status = 2;	
}
#endif

#endif
